package com.hiss.basic.starter.websocket.config;

import com.hiss.basic.starter.websocket.constant.WebSocketConstant;
import com.hiss.basic.starter.websocket.listener.WebSocketMessageListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.listener.ChannelTopic;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.data.redis.listener.adapter.MessageListenerAdapter;

@Configuration
public class WebSocketQueueConfig {

    @Bean
    public RedisMessageListenerContainer container(RedisConnectionFactory connectionFactory,
                                                   MessageListenerAdapter listenerAdapter) {
        RedisMessageListenerContainer container = new RedisMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.addMessageListener(listenerAdapter, webSocketTopic());
        return container;
    }

    @Bean
    public MessageListenerAdapter listenerAdapter(WebSocketMessageListener listener) {
        return new MessageListenerAdapter(listener, "onMessage");
    }

    @Bean
    public WebSocketMessageListener listener() {
        return new WebSocketMessageListener();
    }

    @Bean
    public ChannelTopic webSocketTopic() {
        return new ChannelTopic(WebSocketConstant.WEBSOCKET_QUEUE_TOPIC);
    }
}
